using System;
using System.Xml;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using PickGold;

namespace PickGold.Web
{
    public class CParser : PickGold.Parser
    {
        public CParser() : base() { }
        public CParser(CParser o) : base(o) { }
        public CParser(object o) : base(o) { }
        //
        #region GetJson
        //
		//public static string GetJson(CEntity entity)
		//{
		//    DataSet dataSet = new DataSet();
		//    DataTable dataTable = entity.GetTable();
		//    dataSet.Tables.Add(dataTable.Clone());
		//    for(int i=0;i<dataTable.Rows.Count;i++)
		//        dataSet.Tables[0].Rows.Add(dataTable.Rows[i].ItemArray);
		//    return GetJson(dataSet, entity.EntityName);
		//}
        //
        public static string GetJson(DbDataReader dataReader)
        {
            return GetJson(dataReader, dataReader.GetType().ToString());
        }
        //
        public static string GetJson(DbDataReader dataReader, string name)
        {
            var i = 0;
            var ds = new DataSet();
            ds.Tables.Add(dataReader.GetSchemaTable());
            do
            {
                while (dataReader.Read())
                {
                    var os = new object[dataReader.FieldCount];
                    dataReader.GetValues(os);
                    ds.Tables[i].Rows.Add(os);
                }
                i++;
            } while (dataReader.NextResult());
            return GetJson(ds, name);
        }
        //
        public static string GetJson(DataTable dataTable)
        {
            return GetJson(dataTable, dataTable.TableName);
        }
        //
        public static string GetJson(DataTable dataTable, string name)
        {
            var ds = new DataSet();
            ds.Tables.Add(dataTable.Clone());
            for (var i = 0; i < dataTable.Rows.Count; i++)
                ds.Tables[0].Rows.Add(dataTable.Rows[i].ItemArray);
            return GetJson(ds, name);
        }
        //
        public static string GetJson(DataSet dataSet)
        {
            return GetJson(dataSet, dataSet.DataSetName);
        }
        //
        public static string GetJson(DataSet dataSet, string name)
        {
            if (string.IsNullOrEmpty(name))
                name = dataSet.DataSetName;
            if(string.IsNullOrEmpty(name))
                name = dataSet.GetType().ToString();
            var sb = new StringBuilder(name + " = {children:[\r\n");
            for (var i = 0; i < dataSet.Tables.Count; i++)
            {
                name = dataSet.Tables[i].TableName;
                if(string.IsNullOrEmpty(name))
                    name = dataSet.Tables[i].GetType().ToString();
                sb.Append("\t[\t\n");
                for (var ii = 0; ii < dataSet.Tables[i].Rows.Count; ii++)
                {
                    sb.Append("\t\t{");
                    for (var iii = 0; iii < dataSet.Tables[i].Columns.Count; iii++)
                    {
                        sb.Append(dataSet.Tables[i].Columns[iii].ColumnName + ":\"" +
                            dataSet.Tables[i].Rows[ii][iii].ToString()
                            .Replace("\\", "\\\\")
                            .Replace("\"", "\\\"")
                            .Replace("\'", "\\\'")
                            .Replace("\t", "\\t")
                            .Replace("\r", "\\r")
                            .Replace("\n", "\\n")
                            + "\",");
                    }
                    if (sb[sb.Length - 1] == ',')
                        sb.Remove(sb.Length - 1, 1);
                    sb.Append("},\t\n");
                }
                if (sb[sb.Length - 3] == ',')
                    sb.Remove(sb.Length - 3, 3);
                sb.Append("\r\n\t],\t\n");
            }
            if (sb[sb.Length - 3] == ',')
                sb.Remove(sb.Length - 3, 3);
            sb.Append("\r\n]};\r\n");
            return sb.ToString();
        }
        //
        #endregion
        //
    }
    //
}
//
